<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="formData.userId" placeholder="请输入用户id"/>
        </el-form-item>
        <el-form-item label="中台用户id" prop="ztUserId">
          <el-input v-model="formData.ztUserId" placeholder="请输入中台用户id"/>
        </el-form-item>
        <el-form-item label="订单id" prop="orderId">
          <el-input v-model="formData.orderId" placeholder="请输入订单id"/>
        </el-form-item>
        <el-form-item label="流水号" prop="uuid">
          <el-input v-model="formData.uuid" placeholder="请输入流水号"/>
        </el-form-item>
        <el-form-item label="订单来源" prop="orderSource">
          <el-input v-model="formData.orderSource" placeholder="请输入订单来源"/>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="formData.username" placeholder="请输入用户名"/>
        </el-form-item>
        <el-form-item label="所属部门" prop="department">
          <el-input v-model="formData.department" placeholder="请输入所属部门"/>
        </el-form-item>
        <el-form-item label="所属单位" prop="unit">
          <el-input v-model="formData.unit" placeholder="请输入所属单位"/>
        </el-form-item>
        <el-form-item label="发票类型（个人、企业）" prop="invoiceType">
          <el-select v-model="formData.invoiceType" placeholder="请选择发票类型（个人、企业）">
            <el-option label="请选择字典生成" value=""/>
          </el-select>
        </el-form-item>
        <el-form-item label="发票号码" prop="invoiceNumber">
          <el-input v-model="formData.invoiceNumber" placeholder="请输入发票号码"/>
        </el-form-item>
        <el-form-item label="发票代码" prop="invoiceCode">
          <el-input v-model="formData.invoiceCode" placeholder="请输入发票代码"/>
        </el-form-item>
        <el-form-item label="销方税号-纳税人识别号" prop="sellerTaxno">
          <el-input v-model="formData.sellerTaxno" placeholder="请输入销方税号-纳税人识别号"/>
        </el-form-item>
        <el-form-item label="销方名称" prop="sellerName">
          <el-input v-model="formData.sellerName" placeholder="请输入销方名称"/>
        </el-form-item>
        <el-form-item label="销方电话" prop="sellerTel">
          <el-input v-model="formData.sellerTel" placeholder="请输入销方电话"/>
        </el-form-item>
        <el-form-item label="销方地址" prop="sellerAddress">
          <el-input v-model="formData.sellerAddress" placeholder="请输入销方地址"/>
        </el-form-item>
        <el-form-item label="销方银行名称账号" prop="sellerBankInfo">
          <el-input v-model="formData.sellerBankInfo" placeholder="请输入销方银行名称账号"/>
        </el-form-item>
        <el-form-item label="销方银行名称" prop="sellerBankName">
          <el-input v-model="formData.sellerBankName" placeholder="请输入销方银行名称"/>
        </el-form-item>
        <el-form-item label="销方银行账号" prop="sellerBankAccount">
          <el-input v-model="formData.sellerBankAccount" placeholder="请输入销方银行账号"/>
        </el-form-item>
        <el-form-item label="购方税号" prop="purchaserTaxno">
          <el-input v-model="formData.purchaserTaxno" placeholder="请输入购方税号"/>
        </el-form-item>
        <el-form-item label="购方名称" prop="purchaserName">
          <el-input v-model="formData.purchaserName" placeholder="请输入购方名称"/>
        </el-form-item>
        <el-form-item label="购方地址" prop="purchaserAddress">
          <el-input v-model="formData.purchaserAddress" placeholder="请输入购方地址"/>
        </el-form-item>
        <el-form-item label="购方电话" prop="purchaserTel">
          <el-input v-model="formData.purchaserTel" placeholder="请输入购方电话"/>
        </el-form-item>
        <el-form-item label="购方银行名称账号" prop="purchaserBankInfo">
          <el-input v-model="formData.purchaserBankInfo" placeholder="请输入购方银行名称账号"/>
        </el-form-item>
        <el-form-item label="购方银行名称" prop="purchaserBankName">
          <el-input v-model="formData.purchaserBankName" placeholder="请输入购方银行名称"/>
        </el-form-item>
        <el-form-item label="购方银行账号" prop="purchaserBankAccount">
          <el-input v-model="formData.purchaserBankAccount" placeholder="请输入购方银行账号"/>
        </el-form-item>
        <el-form-item label="税率" prop="taxRate">
          <el-input v-model="formData.taxRate" placeholder="请输入税率"/>
        </el-form-item>
        <el-form-item label="不含税金额" prop="amountNoTax">
          <el-input v-model="formData.amountNoTax" placeholder="请输入不含税金额"/>
        </el-form-item>
        <el-form-item label="税额" prop="taxAmount">
          <el-input v-model="formData.taxAmount" placeholder="请输入税额"/>
        </el-form-item>
        <el-form-item label="含税金额" prop="taxMoney">
          <el-input v-model="formData.taxMoney" placeholder="请输入含税金额"/>
        </el-form-item>
        <el-form-item label="发票开票日期yyyyMMdd" prop="paperdrewDate">
          <el-date-picker clearable v-model="formData.paperdrewDate" type="date" value-format="timestamp"
                          placeholder="选择发票开票日期yyyyMMdd"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="formData.remark" placeholder="请输入备注"/>
        </el-form-item>
        <el-form-item label="收款人姓名" prop="cashierName">
          <el-input v-model="formData.cashierName" placeholder="请输入收款人姓名"/>
        </el-form-item>
        <el-form-item label="复核人姓名" prop="checkerName">
          <el-input v-model="formData.checkerName" placeholder="请输入复核人姓名"/>
        </el-form-item>
        <el-form-item label="开票人姓名" prop="invoicerName">
          <el-input v-model="formData.invoicerName" placeholder="请输入开票人姓名"/>
        </el-form-item>
        <el-form-item label="利润中心" prop="profitCenter">
          <el-input v-model="formData.profitCenter" placeholder="请输入利润中心"/>
        </el-form-item>
        <el-form-item label="公司代码" prop="companyCode">
          <el-input v-model="formData.companyCode" placeholder="请输入公司代码"/>
        </el-form-item>
        <el-form-item label="发票状态(0:作废，1:正常,2:红冲,3:失控,4:异常,5:认证异常,6:红字,默认是1)"
                      prop="invoiceStatus">
          <el-radio-group v-model="formData.invoiceStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="过账状态(0:未过账,1:已过账)" prop="postingStatus">
          <el-radio-group v-model="formData.postingStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="原发票号码" prop="originInvoiceNo">
          <el-input v-model="formData.originInvoiceNo" placeholder="请输入原发票号码"/>
        </el-form-item>
        <el-form-item label="原发票代码" prop="originInvoiceCode">
          <el-input v-model="formData.originInvoiceCode" placeholder="请输入原发票代码"/>
        </el-form-item>
        <el-form-item label="红字信息表编号" prop="redNotificationNo">
          <el-input v-model="formData.redNotificationNo" placeholder="请输入红字信息表编号"/>
        </el-form-item>
        <el-form-item label="审核状态(0:待审核,1:审核中,2:不具备审核条件,3:已审核)" prop="auditStatus">
          <el-radio-group v-model="formData.auditStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="验真标志(0:未验真,2:验真中,3:国税暂无此票,4:验真成功,5:发起验真失败)" prop="taxFlag">
          <el-input v-model="formData.taxFlag"
                    placeholder="请输入验真标志(0:未验真,2:验真中,3:国税暂无此票,4:验真成功,5:发起验真失败)"/>
        </el-form-item>
        <el-form-item label="认证状态(1:认证成功,2:认证中,3:未认证,-1:认证失败)" prop="authStatus">
          <el-radio-group v-model="formData.authStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="认证时间" prop="authTime">
          <el-date-picker clearable v-model="formData.authTime" type="date" value-format="timestamp"
                          placeholder="选择认证时间"/>
        </el-form-item>
        <el-form-item label="认证所属期yyyyMM" prop="taxDeclarationPeriod">
          <el-input v-model="formData.taxDeclarationPeriod" placeholder="请输入认证所属期yyyyMM"/>
        </el-form-item>
        <el-form-item label="PDF下载链接" prop="pdfurl">
          <el-input v-model="formData.pdfurl" placeholder="请输入PDF下载链接"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as DeliveryInvoiceApi from '../../../../api/ipark/delivery/deliveryinvoice';

export default {
  name: "DeliveryInvoiceForm",
  components: {},
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        userId: undefined,
        orderId: undefined,
        uuid: undefined,
        orderSource: undefined,
        username: undefined,
        department: undefined,
        unit: undefined,
        invoiceType: undefined,
        invoiceNumber: undefined,
        invoiceCode: undefined,
        sellerTaxno: undefined,
        sellerName: undefined,
        sellerTel: undefined,
        sellerAddress: undefined,
        sellerBankInfo: undefined,
        sellerBankName: undefined,
        sellerBankAccount: undefined,
        purchaserTaxno: undefined,
        purchaserName: undefined,
        purchaserAddress: undefined,
        purchaserTel: undefined,
        purchaserBankInfo: undefined,
        purchaserBankName: undefined,
        purchaserBankAccount: undefined,
        taxRate: undefined,
        amountNoTax: undefined,
        taxAmount: undefined,
        taxMoney: undefined,
        paperdrewDate: undefined,
        remark: undefined,
        cashierName: undefined,
        checkerName: undefined,
        invoicerName: undefined,
        profitCenter: undefined,
        companyCode: undefined,
        invoiceStatus: undefined,
        postingStatus: undefined,
        originInvoiceNo: undefined,
        originInvoiceCode: undefined,
        redNotificationNo: undefined,
        auditStatus: undefined,
        taxFlag: undefined,
        authStatus: undefined,
        authTime: undefined,
        taxDeclarationPeriod: undefined,
        pdfurl: undefined,
      },
      // 表单校验
      formRules: {},
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await DeliveryInvoiceApi.getDeliveryInvoice(id);
          this.formData = res.data;
          this.title = "修改快递服务-电子发票";
        } finally {
          this.formLoading = false;
        }
      }
      this.title = "新增快递服务-电子发票";
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await DeliveryInvoiceApi.updateDeliveryInvoice(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await DeliveryInvoiceApi.createDeliveryInvoice(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        userId: undefined,
        orderId: undefined,
        uuid: undefined,
        orderSource: undefined,
        username: undefined,
        department: undefined,
        unit: undefined,
        invoiceType: undefined,
        invoiceNumber: undefined,
        invoiceCode: undefined,
        sellerTaxno: undefined,
        sellerName: undefined,
        sellerTel: undefined,
        sellerAddress: undefined,
        sellerBankInfo: undefined,
        sellerBankName: undefined,
        sellerBankAccount: undefined,
        purchaserTaxno: undefined,
        purchaserName: undefined,
        purchaserAddress: undefined,
        purchaserTel: undefined,
        purchaserBankInfo: undefined,
        purchaserBankName: undefined,
        purchaserBankAccount: undefined,
        taxRate: undefined,
        amountNoTax: undefined,
        taxAmount: undefined,
        taxMoney: undefined,
        paperdrewDate: undefined,
        remark: undefined,
        cashierName: undefined,
        checkerName: undefined,
        invoicerName: undefined,
        profitCenter: undefined,
        companyCode: undefined,
        invoiceStatus: undefined,
        postingStatus: undefined,
        originInvoiceNo: undefined,
        originInvoiceCode: undefined,
        redNotificationNo: undefined,
        auditStatus: undefined,
        taxFlag: undefined,
        authStatus: undefined,
        authTime: undefined,
        taxDeclarationPeriod: undefined,
        pdfurl: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>
